package com.hby.game

import android.graphics.Paint
import android.os.Debug
import game.engine.base.*

class ReadyScene : Scene() {


    override fun onTouch(touch: Touch) {
    }

    override fun onEnter() {
        super.onEnter()

        val director = Director.instance
        val graphics = director.graphics
        val wa = director.windowAdapter
        fillBgBitmap = graphics.bgBitmap

        // 标题
        val title = Sprite(bitmap = graphics.titleBitmap)
        addChild(title)

        // info
        val coinInfo = Sprite(bitmap =  graphics.infoBgBitmap).apply {
            x = wa.minX + 140f
            y = wa.minY + HbConfig.InfoOffsetY

            // coin
            val coin = Sprite(bitmap = graphics.coinBitmap)
            coin.x = -100f
            addChild(coin)

            // text
            val text = Label("11")
            text.x = 10f
            text.align = Paint.Align.CENTER
            addChild(text)
        }
        addChild(coinInfo)

        val timeInfo = Sprite(bitmap = graphics.infoBgBitmap).apply {
            x = wa.maxX - 140f
            y = wa.minY + HbConfig.InfoOffsetY

            // time
            val time = Sprite(
                bitmap = graphics.timeBitmap,
                x = -100f
            )
            addChild(time)

            val timeText = Label("倒计时:10s",align = Paint.Align.CENTER)
            addChild(timeText)
        }
        addChild(timeInfo)

        val num = Sprite(bitmap = graphics.wordBitmap3)
        num.visible = false
        addChild(num)

        // 开始倒计时
        scheduleOnce(1f){
            title.removeFromParent()
            num.visible = true
            scheduleCount(1f,3,1f){countIdx->
                when(countIdx){
                    0->num.updateBitmap(graphics.wordBitmap2)
                    1->num.updateBitmap(graphics.wordBitmap1)
                    2->{
                        // 切换到下一个场景
                        UDebug.info("准备切换到下一个场景....")
                        director.replaceScene(GameScene())
                    }
                }
            }
        }


        // 显示内存情况
        UDebug.info("getNativeHeapAllocatedSize mem:${Debug.getNativeHeapAllocatedSize()/1024/1024} M")
        UDebug.info("getNativeHeapFreeSize mem:${Debug.getNativeHeapFreeSize()/1024/1024} M")
        UDebug.info("getNativeHeapSize mem:${Debug.getNativeHeapSize()/1024/1024} M")

        val mem = Runtime.getRuntime().maxMemory() / 1024f / 1024f
        UDebug.info("maxMemory:${mem} M")
    }
}